﻿using System;
using System.IO;
using PPI.Client.Interface;
using PPI.Client.Logic;
using PPI.Entities.Settings;

namespace PPI.Client.Presenter
{
    public class DeployDistributionDatabasePresenter : DeployDatabasePresenterBase
    {
        #region - Constructor -

        public DeployDistributionDatabasePresenter(IDeployDatabase view) : base(view)
        {
        }

        #endregion

        #region - Protected Methods -

        /// <summary>
        /// Sets application files to download
        /// </summary>
        protected override void SetApplicationFiles()
        {
            m_LocalFiles.Push(Constants.K_LOCALAPPLICATIONDISTRIBUTIONMOBILE);
            m_WebFiles.Push(Constants.K_WEBAPPLICATIONDISTRIBUTIONMOBILE);
        }

        /// <summary>
        /// Checks if downloaded files are updated, if true, they'll be deleted.
        /// </summary>
        protected override void CheckFilesAreUpdate()
        {
            bool filesToDelete;

            try
            {
                filesToDelete = LSettings.CheckLocalSettingsWithServer(new ESettingDeploymentMobileDistribution());
            }
            catch (Exception)
            {
                filesToDelete = true;
            }

            if (filesToDelete && File.Exists(Constants.K_LOCALAPPLICATIONDISTRIBUTIONMOBILE))
                File.Delete(Constants.K_LOCALAPPLICATIONDISTRIBUTIONMOBILE);
        }

        #endregion

        #region - Public Methods -

        /// <summary>
        /// Deploys a new Distribution App on local machine
        /// </summary>
        /// <param name="distributionAppMode">Mode to deploy</param>
        /// <returns>Complete path where the app was deployed</returns>
        public string DeployDistribution(int distributionAppMode)
        {
            m_view.IsDownloading = true;
            m_SettingsBase = new ESettingDeploymentMobileDistribution();
            m_SettingsBase = LSettings.GetSetting(m_SettingsBase);
            
            string sourceDatabase = LDeploy.StartDeployDistribution(distributionAppMode);
            m_view.IsDownloading = false;

            return LDeploy.ParseDownloadDatabase(sourceDatabase);
        }


        public string DeployDistribution(int vendorId, string vendorName, int vendorContactId, string vendorContactName, int distributionAppMode, int vendorProjectId, int utilityId, string utilityName)
        {
            m_view.IsDownloading = true;
            m_SettingsBase = new ESettingDeploymentMobileDistribution();
            m_SettingsBase = LSettings.GetSetting(m_SettingsBase);

            string sourceDatabase = LDeploy.StartDeployDistribution(vendorId, vendorName, vendorContactId, vendorContactName, distributionAppMode, vendorProjectId, utilityId, utilityName);
            m_view.IsDownloading = false;

            return LDeploy.ParseDownloadDatabase(sourceDatabase);
        }

        #endregion
    }
}
